package com.ssm.service;

import com.ssm.mapper.TaskInfoMapper;
import com.ssm.mapper.TaskMapper;
import com.ssm.po.Task;
import com.ssm.po.TaskInfo;
import com.ssm.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskInfoService {


    @Autowired
    TaskInfoMapper taskInfoMapper;

    @Resource(name="taskMapper")
    TaskMapper taskMapper;

    /**
     * 添加时间轴信息
     */
    public void addTaskInfo(TaskInfo taskInfo){
        taskInfoMapper.insert(taskInfo) ;
    }

    /**
     * 更新时间轴信息
     */
    public void updataTaskInfo(TaskInfo taskInfo){
        if (taskInfo==null||taskInfo.getTaskid()==null){
            return;
        }
        TaskInfo taskInfo1 = taskInfoMapper.selectByPrimaryKey(taskInfo.getTaskid());

        if (taskInfo1.getTaketime()==null){
            taskInfo1.setTaketime(taskInfo.getTaketime());
        }
        if (taskInfo1.getAboutovertime()==null){
            taskInfo1.setAboutovertime(taskInfo.getAboutovertime());
        }
        if (taskInfo1.getOvertime()==null){
            taskInfo1.setOvertime(taskInfo.getOvertime());
        }
        if (taskInfo1.getEndtime()==null){
            taskInfo1.setEndtime(taskInfo.getEndtime());
        }
        if (taskInfo1.getCancletime()==null){
            taskInfo1.setCancletime(taskInfo.getCancletime());
        }
        if (taskInfo1.getEvaluatescore()==null){
            taskInfo1.setEvaluatescore(taskInfo.getEvaluatescore());
        }
        if (taskInfo1.getEvaluate()==null){
            taskInfo1.setEvaluate(taskInfo.getEvaluate());
        }
        taskInfoMapper.updateByPrimaryKey(taskInfo1);
    }

    /**
     * 查看时间轴信息
     */
    public TaskInfo getTaskInfo(int taskId){
       return taskInfoMapper.selectByPrimaryKey(taskId);
    }

    /**
     * 设置大概需要时间
     *
     * @param taskId
     * @param aboutUse 大概需要时间
     * @return
     */
    public void aboutUse(int taskId, String aboutUse) {
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setTaskid(taskId);
        taskInfo.setAboutovertime(aboutUse);
        taskInfo.setTaketime(TimeUtil.getTime());
        updataTaskInfo(taskInfo);
    }

    public void evaluate(int taskId, String evaluateScore, String evaluate) {

        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setTaskid(taskId);
        taskInfo.setEvaluate(evaluate);
        taskInfo.setEvaluatescore(evaluateScore);
        updataTaskInfo(taskInfo);
    }

    public List<Task> findAccept(int userId) {
        List<Task>  list = new ArrayList<>();
        List<Task> tasks = taskMapper.selectTheAllTask();
        for (Task task:tasks){
            if (task.getAcceptUserId()==userId){
                list.add(task);
                System.out.println("findAccept"+task);
            }
        }
        return list;
    }
    public List<Task> findOrder(int userId) {
        List<Task>  list = new ArrayList<>();
        List<Task> tasks = taskMapper.selectTheAllTask();
        for (Task task:tasks){
            if (task.getPublishUserId().equals(userId+"")){
                list.add(task);
                System.out.println(task.toString());
                System.out.println(task);
            }
        }
        System.out.println(list.toString());
        return list;
    }
}
